/*
 * Created by JFormDesigner on Thu May 27 11:06:18 CST 2021
 */

package renthouse.view.information.admin;

import renthouse.dao.OrderDao;
import renthouse.entry.Order;
import renthouse.view.mainManger.AdminFrame;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

/**
 * @author Brainrain
 */
public class AdminFindOrder extends JFrame {
    public static void main(String[] args) {
        AdminFindOrder adminFindOrder=new AdminFindOrder();
        adminFindOrder.setVisible(true);
    }
    public AdminFindOrder() {
        initComponents();
        myTableInit();
    }

    OrderDao orderDao=new OrderDao();

    public static int pageNum = 1;
    public static int pageSize = 4;
    public static int allPage = 0;
    public static int allCount = 0;
    public void myTableInit() {
        String orderNumber = ordernumber.getText();
        Order order=new Order();
        OrderDao orderDao=new OrderDao();

        if (orderNumber != null &&!"".equals(orderNumber)) {
            order.setNumber(Integer.valueOf(orderNumber));
        }
        String[] ss = new String[]{"订单编号", "房屋ID","房东ID", "房客ID", "地址", "状态", "风格", "每月租金","开始租房时间","退房时间"};


        List <Order> list = orderDao.queryForPage(order, pageNum, pageSize);
        int len = 0;
        if (list != null || list.size() > 0) {
            len = list.size();
        }
        Object[][] objects = new Object[len][10];//定义每列的值
        for (int i = 0; i < list.size(); i++) {
            Order order1 = list.get(i);
            Object[] objects1 = new Object[]{order1.getNumber(),
                    order1.getHouseid(), order1.getMasterid(),
                    order1.getUserid(), order1.getAddress(), order1.getState(),
                    order1.getStyle(), order1.getRent(),order1.getStart(),order1.getEnd()};
            objects[i] = objects1;
        }
        table1.setModel(new DefaultTableModel(objects, ss));

        table1.getColumnModel().getColumn(0).setMaxWidth(50); //设置宽度
        table1.getColumnModel().getColumn(1).setMaxWidth(50); //设置宽度
        table1.getColumnModel().getColumn(2).setMaxWidth(50); //设置宽度
        table1.getColumnModel().getColumn(3).setMaxWidth(50); //设置宽度

        allCount = orderDao.queryCount(order);

        if (allCount % pageSize == 0) {
            allPage = allCount / pageSize;
            if (allCount == 0) {
                allPage = 1;
            }
        } else {
            allPage = (allCount / pageSize) + 1;
        }

        label2.setText("第 " + pageNum + " 页/共 " + allPage + " 页   每页 " + pageSize + " 条/共 " + allCount + " 条");
    }


    private void button1ActionPerformed() {
        // TODO add your code here
        pageNum = 1;
        myTableInit();

    }

    private void buttonshangActionPerformed(ActionEvent e) {
        // TODO add your code here
        if (pageNum == 1) {
            JOptionPane.showMessageDialog(null, "已经是第一页了");
            return;
        }
        pageNum--;
        myTableInit();

    }

    private void buttonxiaActionPerformed(ActionEvent e) {
        // TODO add your code here
        if (pageNum == allPage) {
            JOptionPane.showMessageDialog(null, "已经最后一页了");
            return;
        }
        pageNum++;
        myTableInit();

    }

    private void buttonweiActionPerformed(ActionEvent e) {
        // TODO add your code here
        pageNum = allPage;
        myTableInit();
    }

    private void buttonshouActionPerformed() {
        // TODO add your code here
        pageNum = 1;
        myTableInit();
    }

    private void returnmainActionPerformed() {
        // TODO add your code here
        setVisible(false);
        AdminFrame adminFrame=new AdminFrame();
        adminFrame.setVisible(true);

    }

    private void deleteitemActionPerformed() {
        // TODO add your code here
        int selectRow = table1.getSelectedRow(); //如果得到的值是-1 表示没有选中行
        if (selectRow == -1) {
            JOptionPane.showMessageDialog(null, "请选择您需要删除的数据");
            return;
        }
        int houseId = (int) table1.getValueAt(selectRow, 0);

        boolean f = orderDao.deleteById(houseId);

        if (f) {
            JOptionPane.showMessageDialog(null, "删除成功");
        } else {
            JOptionPane.showMessageDialog(null, "删除删除失败");
        }

        myTableInit();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        menuBar1 = new JMenuBar();
        menu2 = new JMenu();
        deleteitem = new JMenuItem();
        menu3 = new JMenu();
        returnmain = new JMenuItem();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        panel2 = new JPanel();
        buttonshang = new JButton();
        buttonxia = new JButton();
        buttonwei = new JButton();
        label2 = new JLabel();
        buttonshou = new JButton();
        panel3 = new JPanel();
        ordernumber = new JTextField();
        button1 = new JButton();
        label1 = new JLabel();

        //======== this ========
        setTitle("\u7ba1\u7406\u5458\u67e5\u8be2\u8ba2\u5355");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== menuBar1 ========
        {

            //======== menu2 ========
            {
                menu2.setText("\u8ba2\u5355\u64cd\u4f5c");

                //---- deleteitem ----
                deleteitem.setText("\u5220\u9664\u8ba2\u5355");
                deleteitem.addActionListener(e -> deleteitemActionPerformed());
                menu2.add(deleteitem);
            }
            menuBar1.add(menu2);

            //======== menu3 ========
            {
                menu3.setText("\u5176\u4ed6");

                //---- returnmain ----
                returnmain.setText("\u8fd4\u56de\u4e3b\u83dc\u5355");
                returnmain.addActionListener(e -> returnmainActionPerformed());
                menu3.add(returnmain);
            }
            menuBar1.add(menu3);
        }
        setJMenuBar(menuBar1);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table1 ----
                table1.setModel(new DefaultTableModel(
                    new Object[][] {
                        {"1", "2"},
                        {null, null},
                    },
                    new String[] {
                        null, null
                    }
                ));
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
            scrollPane1.setBounds(0, 50, 795, 360);

            //======== panel2 ========
            {
                panel2.setLayout(null);

                //---- buttonshang ----
                buttonshang.setText("\u4e0a\u4e00\u9875");
                buttonshang.addActionListener(e -> buttonshangActionPerformed(e));
                panel2.add(buttonshang);
                buttonshang.setBounds(new Rectangle(new Point(525, 15), buttonshang.getPreferredSize()));

                //---- buttonxia ----
                buttonxia.setText("\u4e0b\u4e00\u9875");
                buttonxia.addActionListener(e -> buttonxiaActionPerformed(e));
                panel2.add(buttonxia);
                buttonxia.setBounds(600, 15, 80, 25);

                //---- buttonwei ----
                buttonwei.setText("\u5c3e\u9875");
                buttonwei.addActionListener(e -> buttonweiActionPerformed(e));
                panel2.add(buttonwei);
                buttonwei.setBounds(700, 15, 80, 25);

                //---- label2 ----
                label2.setText("\u7b2c 1 \u9875/\u5171 1 \u9875   \u6bcf\u9875 4 \u6761/\u5171 12 \u6761");
                panel2.add(label2);
                label2.setBounds(15, 20, 225, 17);

                //---- buttonshou ----
                buttonshou.setText("\u9996\u9875");
                buttonshou.addActionListener(e -> buttonshouActionPerformed());
                panel2.add(buttonshou);
                buttonshou.setBounds(new Rectangle(new Point(440, 15), buttonshou.getPreferredSize()));

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel2.getComponentCount(); i++) {
                        Rectangle bounds = panel2.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel2.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel2.setMinimumSize(preferredSize);
                    panel2.setPreferredSize(preferredSize);
                }
            }
            panel1.add(panel2);
            panel2.setBounds(5, 420, 785, 50);

            //======== panel3 ========
            {
                panel3.setLayout(null);
                panel3.add(ordernumber);
                ordernumber.setBounds(100, 5, 225, 24);

                //---- button1 ----
                button1.setText("\u67e5\u8be2");
                button1.addActionListener(e -> button1ActionPerformed());
                panel3.add(button1);
                button1.setBounds(new Rectangle(new Point(360, 5), button1.getPreferredSize()));

                //---- label1 ----
                label1.setText("\u8ba2\u5355\u7f16\u53f7");
                panel3.add(label1);
                label1.setBounds(20, 10, 65, label1.getPreferredSize().height);

                { // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel3.getComponentCount(); i++) {
                        Rectangle bounds = panel3.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel3.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel3.setMinimumSize(preferredSize);
                    panel3.setPreferredSize(preferredSize);
                }
            }
            panel1.add(panel3);
            panel3.setBounds(0, 5, 790, 45);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 0, 795, 475);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JMenuBar menuBar1;
    private JMenu menu2;
    private JMenuItem deleteitem;
    private JMenu menu3;
    private JMenuItem returnmain;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel panel2;
    private JButton buttonshang;
    private JButton buttonxia;
    private JButton buttonwei;
    private JLabel label2;
    private JButton buttonshou;
    private JPanel panel3;
    private JTextField ordernumber;
    private JButton button1;
    private JLabel label1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
